{include file="public/page_header" /} <!--头部公共区域-->

<style>
    .layui-form-label{
        width: 90px;
    }
    .layui-input-block{
        margin-left: 120px;
    }
    .layui-btn.layui-tijiao{
        background-color: #224a9b;
        border-radius: 4px;
    }
</style>
<div class="layui-layout layui-layout-admin" style="margin: 20px;">
    <form action="" class="layui-form">
        <input type="hidden" {if !empty($data) }value="{$data.id}"{/if} name="id">
        <div class="layui-form-item clearfix">
            <label class="layui-form-label">父级菜单</label>
            <div class="layui-input-inline">
                <select name="parent" lay-verify="required" lay-filter="rule-select">
                    <option value="0">无父级菜单</option>
                    {foreach name = "first_menu" item = "vo"}
                    <option value="{$vo.id}" {if condition="!empty($data) && $data['parent']==$vo['id']"}selected{/if}>{$vo.title}---{$vo.name}</option>
                    {/foreach}
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">菜单名称</label>
            <div class="layui-input-block">
                <input type="text" name="title" required  lay-verify="required" autocomplete="off" class="layui-input input-w" {if !empty($data) }value="{$data.title}"{/if}>
            </div>
        </div>
<!--        <div class="layui-form-item">-->
<!--            <label class="layui-form-label">菜单URL</label>-->
<!--            <div class="layui-input-block">-->
<!--                <input type="text" name="name" required  lay-verify="required" autocomplete="off" class="layui-input input-w" {if !empty($data) }value="{$data.name}"{/if}>-->
<!--            </div>-->
<!--        </div>-->
        <div class="layui-form-item">
            <label class="layui-form-label">模块</label>
            <div class="layui-input-block">
                <input type="text" name="module" value="{$data.module}" lay-verify="required" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">控制器</label>
            <div class="layui-input-block">
                <select name="controller" autocomplete="off" lay-filter="controller">
                    <option value="#">---</option>
                    {volist name="controller" id="vo" key="k"}
                    <option value="{$vo}" {if $vo == $data.controller} selected {/if} >{$vo}</option>
                    {/volist}
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">方法</label>
            <div class="layui-input-block">
                <select name="action" id="action" lay-filter="action" autocomplete="off">
                    <option value="#">---</option>
                    {volist name="action" id="vo" key="k"}
                    <option value="{$vo}" {if $vo == $data.action} selected {/if} >{$vo}</option>
                    {/volist}
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">类型-默认菜单</label>
            <div class="layui-input-block">
                <!--              <input type="checkbox" name="type" {if !empty($data) }{eq name="$data['type']" value="1" }checked=""{/eq}{/if} lay-skin="switch" lay-text="按钮|菜单">-->
                <input type="radio" name="type" value="0" lay-verify="required" autocomplete="off" title="菜单" {if $data.type==0}checked{/if}>
                <input type="radio" name="type" value="1" lay-verify="required" autocomplete="off" title="按钮" {if $data.type==1}checked{/if}>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">开关-默认关</label>
            <div class="layui-input-block">
                <!--<input type="checkbox" name="status" {if !empty($data) }{eq name="$data['status']" value="0" }checked=""{/eq}{/if} lay-skin="switch" lay-text="开|关">-->
                <input type="radio" name="status" value="1" lay-verify="required" autocomplete="off" title="开" {if $data.status==1}checked{/if}>
                <input type="radio" name="status" value="0" lay-verify="required" autocomplete="off" title="关" {if $data.status==0}checked{/if}>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">排序</label>
            <div class="layui-input-block">
                <input type="number" name="sort" required  lay-verify="required" autocomplete="off" class="layui-input input-w" {if !empty($data) }value="{$data.sort}"{/if}>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">图标</label>
            <div class="layui-input-block">
                <input type="text" id="iconPicker" lay-filter="iconPicker" class="layui-hide" >
                <input id="icon" name="icon" value="{$data.icon}" autocomplete="off" class="layui-input" >
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-tijiao" lay-submit lay-filter="formDemo" style="background: #5470c6;">立即提交</button>
            </div>
        </div>
    </form>
</div>

{include file="public/page_footer" /} <!--尾部公共区域-->

<script type="text/javascript">

    layui.use('form', function(){
        var form = layui.form;
        form.on('select(controller)', function(data){
            var controller = data.value;
            if (controller) {
                $.ajax({
                    type : "POST",  //提交方式
                    url : "{:url('menu/getActionList')}",//路径
                    data : {controller:controller},
                    dataType: "json",
                    success: function(data){
                        console.log(data);
                        var html = '<option value="#">---</option>';
                        $.each(data.data,function (index,item) {
                            html += '<option value="'+item+'">'+item+'</option>';
                        });
                        $("#action").children().remove();
                        $("#action").append(html);
                        form.render();
                        return false;
                    }
                });
            }
        });
        // form.on('switch(buttons)', function(data){
        //     if(this.checked){
        //         this.value = 1;
        //     }else{
        //         this.value = 0;
        //     }
        // });
        // form.on('switch(switchs)', function(data){
        //     if(this.checked){
        //         this.value = 0;
        //     }else{
        //         this.value = 1;
        //     }
        // });
        //监听提交
        form.on('submit(formDemo)', function(data){
            var dataParam = $("form").serializeArray();
            $.ajax({
                type : "POST",  //提交方式
                url : "{:url('menu/edit')}",//路径
                data : dataParam,
                success: function(data){
                    top.layer.msg(data.msg)
                    if(data.code == 0){
                        setTimeout(function () {
                            $(window.parent.document).find("iframe")[0].contentWindow.location.reload(true);
                            var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                            parent.layer.close(index);
                        }, 2000);
                    }
                }
            });
            return false;
        });

    });

</script>
